package com.longrise.android.byjk.plugins.aboutme.personalInfo.product_show;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.WindowManager;
import android.widget.EditText;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Administrator on 2018/3/9.
 * 新增产品方案
 */

public class AddProductProActivity extends BaseActivity2<AddProductProPresenter> implements AddProductProContract.View, TextWatcher {

    public static final String PRODUCTID = "productid";
    public static final String PRODUCTNAME = "productname";
    public static final String PRODUCTDESC = "productdesc";
    private EditText mEtName;
    private EditText mEtDesc;
    private String save;
    private String mProductId;
    private String mProductName;
    private String mProductdesc;
    private String mEt1Length;
    private String mEt2Length;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_product_program_add;
    }

    @Override
    public void initView() {
        mEtName = (EditText) findViewById(R.id.et_product_add_name);
        mEtDesc = (EditText) findViewById(R.id.et_product_add_desc);
        setToolbarTitle(AppUtil.getString(R.string.person_honor));
        save = AppUtil.getString(R.string.savepic);
        initEvent();
        getExtraData();
        setToolbarRightTextTitleUnable(save, Color.parseColor("#FFB3B3B3"));
    }

    public void initEvent(){
        mEtName.addTextChangedListener(this);
        mEtDesc.addTextChangedListener(this);
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    @Override
    public void onToolbarRightTvClick() {
        super.onToolbarRightTvClick();
        if(TextUtils.isEmpty(mProductId)){
            mPresenter.addProductData(mEt1Length,mEt2Length);
        }else{
            mPresenter.modifyProductData(mProductId,mEt1Length,mEt2Length);
        }
    }

    public void getExtraData() {
        Intent intent = getIntent();
        mProductId = intent.getStringExtra(PRODUCTID);
        mProductName = intent.getStringExtra(PRODUCTNAME);
        mProductdesc = intent.getStringExtra(PRODUCTDESC);
        Control.setProductName(mProductName);
        Control.setProductDesc(mProductdesc);
        mEtName.setText(Control.getProductName());
        mEtDesc.setText(Control.getProductDesc());
        if (!TextUtils.isEmpty(Control.getProductName())) {
            mEtName.setSelection(Control.getProductName().length());
        }
        softKey(mEtName);
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

        mEt1Length = mEtName.getText().toString();
        mEt2Length = mEtDesc.getText().toString();

        if(TextUtils.isEmpty(mEt1Length) || TextUtils.isEmpty(mEt2Length)){
            setToolbarRightTextTitleUnable(save, Color.parseColor("#FFB3B3B3"));
        }else {
            if(mEt1Length.equals(Control.getProductName()) && mEt2Length.equals(Control.getProductDesc())){
                setToolbarRightTextTitleUnable(save, Color.parseColor("#FFB3B3B3"));
            }else{
                setToolbarRightTextTitle(save);
            }
        }
    }

    @Override
    public void refreshData(EntityBean bean) {
        String showname = bean.getString("showname");
        ProductProEvent event = new ProductProEvent();
        event.setRefresh(true);
        event.setShowname(showname);
        EventBus.getDefault().post(event);
        UserInfor.getInstance().setUserCPZS(showname);
        finish();
    }

    private void softKey(EditText editText) {
        try {
            editText.requestFocus();
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setVisible(boolean isVisible){
        if(isVisible) {
            setToolbarRightTextTitle(save);
        }else{
            setToolbarRightTextTitleUnable(save, Color.parseColor("#FFB3B3B3"));
        }
    }
}
